{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4d8583d3",
   "metadata": {},
   "source": [
    "# list的使用方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "997cd4c6",
   "metadata": {},
   "source": [
    "## 2.增加/新增数据，stu_网新1班 =[...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d5d11f18",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "636a6a83",
   "metadata": {},
   "source": [
    "### 2.1 append() 圆括号的内容是 对象（比较大的一个范围，python中一切皆是对象）\n",
    "###     append() 新增在末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "76feb551",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'cheng', 'qi']\n"
     ]
    }
   ],
   "source": [
    "names.append(\"cheng\")\n",
    "names.append(\"qi\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ebae053",
   "metadata": {},
   "source": [
    "### 2.2 insert() 指定位置新增数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6ad9d270",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'jia', 'Jan', 'Jack', 'cheng', 'qi']\n"
     ]
    }
   ],
   "source": [
    "names.insert(2,\"jia\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77947e1b",
   "metadata": {},
   "source": [
    "## 3.删除\n",
    "### 3.1 remove() 的操作应该先做查询，判断元素value是否存在？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "566f651a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'jia', 'Jan', 'Jack', 'cheng']\n"
     ]
    }
   ],
   "source": [
    "names.remove(\"qi\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7bf3a8ee",
   "metadata": {},
   "source": [
    "#### * 先做查询： seq的一般方法（通用方法）\n",
    "#### print(\"lihua\" in names) # False/True\n",
    "#### username = input(\"names:\")\n",
    "#### if username in names:\n",
    "####     * 删除该用户\n",
    "####     names.remove(username)\n",
    "#### else:\n",
    "####     names.append(username)\n",
    "#### print(names)\n",
    "#### names.remove(\"lihua\")\n",
    "#### print(names)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ce475f3",
   "metadata": {},
   "source": [
    "## 3.2 了解list的长度， 一般方法 len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "83a25a41",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "['Mike', 'Mary', 'jia', 'Jan', 'Jack', 'cheng']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'cheng']\n"
     ]
    }
   ],
   "source": [
    "print(len(names))\n",
    "print(names)\n",
    "names.pop(2)\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f3baa30",
   "metadata": {},
   "source": [
    "#### 清楚删除的索引位置?\n",
    "#### print(names.index(\"Jan\"))\n",
    "#### 案例： 删除一些具有特征的索引值\n",
    "#### for i in range(0,len(names)-1,2):\n",
    "####     names.pop(i)\n",
    "#### print(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6aefe383",
   "metadata": {},
   "outputs": [],
   "source": [
    "names_网新1 = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "names_网新2 = [\"Alex\",\"Zhichao\",\"Lihua\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f7622d8",
   "metadata": {},
   "source": [
    "## 4.删除列表\n",
    "#### names.clear()\n",
    "#### print(names)\n",
    "\n",
    "## 5.合并列表 extend 是有顺序的，（）里面的list排在后面\n",
    "#### names_网新2.extend(names_网新1)\n",
    "#### print(names_网新2)\n",
    "\n",
    "## 6. copy() 复制列表  赋值 不等于 真正意义的复制\n",
    "#### names_网新1复制 = names_网新1\n",
    "#### print(names_网新1)\n",
    "#### print(names_网新1复制)\n",
    "#### names_网新1.append(\"youge\")\n",
    "#### print(\"names_网新1:\",names_网新1)\n",
    "#### print(\"names_网新1复制:\",names_网新1复制)\n",
    "\n",
    "## 引入copy()\n",
    "\n",
    "#### names_网新1_源数据备份 = names_网新1.copy()\n",
    "#### print(names_网新1)\n",
    "#### print(names_网新1_源数据备份)\n",
    "#### names_网新1.append(\"youge\")\n",
    "#### print(\"names_网新1:\",names_网新1)\n",
    "#### print(\"names_网新1_copy:\",names_网新1_源数据备份)\n",
    "\n",
    "#### 用切片 [:] 等价于 copy()方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbf6ba5c",
   "metadata": {},
   "source": [
    "#### names_网新1_源数据备份 = names_网新1.copy()\n",
    "#### print(names_网新1)\n",
    "#### print(names_网新1_源数据备份)\n",
    "#### names_网新1.append(\"youge\")\n",
    "#### print(\"names_网新1:\",names_网新1)\n",
    "#### print(\"names_网新1_copy:\",names_网新1_源数据备份)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ede4889b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack', 'youge']\n",
      "['Mike', 'Mary', 'Jan', 'Jack', 'youge']\n",
      "names_网新1: ['Mike', 'Mary', 'Jan', 'Jack', 'youge', 'youge']\n",
      "names_网新1_源数据备份: ['Mike', 'Mary', 'Jan', 'Jack', 'youge']\n"
     ]
    }
   ],
   "source": [
    "names_网新1_源数据备份 = names_网新1[:]\n",
    "print(names_网新1)\n",
    "print(names_网新1_源数据备份)\n",
    "names_网新1.append(\"youge\")\n",
    "print(\"names_网新1:\",names_网新1)\n",
    "print(\"names_网新1_源数据备份:\",names_网新1_源数据备份)\n",
    "stu_id = [95,97,90,90,97,88,90]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "264392fd",
   "metadata": {},
   "source": [
    "## 7. count() 查看元素出现的次数/统计数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "21923a29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "print(stu_id.count(90))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "880a9ce2",
   "metadata": {},
   "source": [
    "## 8. sort() 排序 reverse=True 从高到低，默认从低到高排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "81a6296f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[97, 97, 95, 90, 90, 90, 88]\n",
      "['Jack', 'Jan', 'Mary', 'Mike', 'youge', 'youge']\n"
     ]
    }
   ],
   "source": [
    "stu_id.sort(reverse=True)\n",
    "print(stu_id)\n",
    "\n",
    "names_网新1.sort() # 字符串排序方法是 ： 首字母的a-z顺序\n",
    "print(names_网新1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad98a88d",
   "metadata": {},
   "source": [
    "# 课本练习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcc794ea",
   "metadata": {},
   "source": [
    "## 处理seq types代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4bad2d0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # 把字符串转变为 list ： list(str)\n",
    "print(phrase)\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5e741868",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):# 循环4次 进行四次plist的pop()方法操作，即删除4次末位\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "print(plist)\n",
    "plist.extend([plist.pop(),plist.pop()])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cbcb49b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "plist.insert(2,plist.pop(3))# 删除 位置3的 ‘ ’ 并且插入到  位置 2\n",
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "aa04d52c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "plist= ['o', 'n', ' ', 't', 'a', 'p']\n",
      "new_phrase= on tap\n"
     ]
    }
   ],
   "source": [
    "new_phrase = ''.join(plist) # 列表元素的合并\n",
    "print(\"plist=\",plist)\n",
    "print(\"new_phrase=\",new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e4e5ea2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
